// pages/user/money.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:3,
    showAddBank: 0,
    user_info:{},
    array: ['招商银行', '北京银行'],
    bankname: '',
    selectBank:{},
    page:1,
    lastpage:0,
    loading:0,
    moneyLog:[],
    myBanks:[],
    payList:[],
    payListSelect:[],
    payListSelectKey:0,
    payListSelectText:'请选择',
  },
  bindChange: function (e) {
    this.data.payListSelectKey = e.detail.value;
    this.setData({ payListSelectText: this.data.payListSelect[e.detail.value] });
    console.log(e);
  },

  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var user_info = wx.getStorageSync('user_info');
    // this.setData({ user_info: user_info});
    // //console.log(options);
    this.setData({ extConfig: app.getExtConfigSync() })
     this.getInfo();
     this.loadmoney();
    //充多少送多少
    this.payList();
  },
  getInfo:function(){
    var that = this;
    app.api("api/user/info", {
      fields:"avatarUrl,user_id,last_update_time,nickname"
    }, function (res) {
      
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      var income=(res.data.income*0.99).toFixed(2);
      var fee = (res.data.income * 0.01).toFixed(2);
      that.setData({ fee: fee, income:income,user_info: res.data });
    })  
  },
//充值选项 
  payList:function(){
    var that = this;
    app.api("mall/promotion/find", {
      name: 'recharge'
    }, function (data) {
      if (data.code != 200) {
        app.alert(data.msg);
        return;
      }
      //console.log(data.data.name);
      var payListSelect =[];
      for (var i in data.data.config.set){
        payListSelect.push("充" + data.data.config.set[i].recharge + "送" + data.data.config.set[i].present);
      }
      that.setData({
        payList: data.data,
        payListSelect: payListSelect
      });

    })
  },
  //资金流水 
  loadmoney:function(reload=0){
    var that = this;
    
    if (reload==1){
      this.setData({
        loading:0,
        page:1,
        lastpage:0,
        moneyLog:[],
      });
    }
    
    if (this.data.loading==1 || this.data.lastpage==1){
      return ;
    }
    var page = this.data.page;
    this.data.loading=1;
    app.api("api/money_log/get", {
      page: page,
      limit:10,
      order:'id desc'
    },function(res){
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length==0){
        that.data.lastpage=1;
      }
      that.data.page++;
      that.setData({
        moneyLog: that.data.moneyLog.concat(res.data.list),
      });
    })
  },
  showView:function(e){
    this.setViweType(e.currentTarget.dataset.id );
    console.log(e.currentTarget.dataset.id);
  },
  setViweType:function(id){
    this.setData({ type: id });
    // var title ="我的总收入";
    // // if(id<4){
    // //   this.setData({ moneyLog: [], page: 1 });
    // //   this.loadmoney();
    // // }
    // if(id==2){
    //   title="未结算的金额";
    // }else if(id==3){
    //   title = "我的计划金";
    // }else if(id ==4){
    //   title = "提现到微信零钱";
    // }else if(id == 5){
    //   title = "银行卡管理";
    // }else{
    //   title = "我的计划金";
    // }
    // wx.setNavigationBarTitle({
    //   title: title
    // })
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  unDoWithDraw:function(){
    this.setViweType(3);
    //setViweType(3)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
//充值
  doRecharge:function(){

    var that = this;
    that.setData({ loading: 1, disabled: 1 });
    that.data.payloading = 1;
    app.api("/api/pay/recharge", {
      recharge_id: this.data.payListSelectKey,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        that.data.payloading = 0;
        that.setData({ 'loading': 0 });
        that.setData({ 'disabled': 0 });
        return false;
      }
      var order_sn = res.data.order_sn;
      app.api("api/pay/topay", {
        order_sn: order_sn
      }, function (res) {
        if (res.code != 200) {
          app.alert(res.msg);
          that.data.payloading = 0;
          that.setData({ 'loading': 0 });
          that.setData({ 'disabled': 0 });
          return false;
        }
        
        var params = res.data;
        //params.total_fee = 0.1;
        params.success = function (res) {
          that.setData({ loading: 0, disabled: 0 });
          that.unDoWithDraw();
          that.onPullDownRefresh();
        }
        params.fail = function (res) {
          console.log(res);
          if (res.errMsg == 'requestPayment:fail cancel') {
            that.setData({ loading: 0, disabled: 0 });
            return false;
          }
          app.alert(res.errMsg);
        }
        console.log(params);
        wx.requestPayment(params)
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getInfo();
    this.loadmoney(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadmoney();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})